package org.jeecg.modules.mobile.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseArea;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseAreaService;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.mobile.entity.vo.MobileWarehouseVO;
import org.jeecg.modules.mobile.mapper.MobileMoveMapper;
import org.jeecg.modules.mobile.service.IMobileMoveReceiptService;
import org.jeecg.modules.mobile.service.IMobileMyTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Description: 移动端-移库
 * @Author: jeecg-boot
 * @Date:   2023-09-21
 * @Version: V1.0
 */
@Service
public class MobileMoveReceiptServiceImpl  implements IMobileMoveReceiptService {

    @Autowired
    private IMobileMyTaskService mobileMyTaskService;

    @Autowired
    private IWmsRepertoryService repertoryService;

    @Autowired
    private MobileMoveMapper mobileMoveMapper;

    @Autowired
    private IWmsBasicWarehouseAreaService warehouseAreaService;

    @Autowired
    private IWmsMoveReceiptService moveReceiptService;

    /**
     * 扫描物料条码或者库区/库位，查询即时库存
     * @param name
     * @param warehouseId
     * @return
     */
    @Override
    public List<WmsRepertory> queryMaterial(String name, String warehouseId) {
        if (StringUtils.isBlank(name)) {
            return Collections.emptyList();
        }
        List<WmsRepertory> list;
        if (name.startsWith("KQ") || name.startsWith("KW")) {
            MobileWarehouseVO vo = mobileMyTaskService.queryWarehouse(name, warehouseId);
            list = repertoryService.list(new LambdaQueryWrapper<WmsRepertory>()
                    .eq(WmsRepertory::getWmsBasicWarehouseId, warehouseId)
                    .eq(StringUtils.isNotBlank(vo.getWarehouseAreaId()), WmsRepertory::getWmsBasicWarehouseAreaId, vo.getWarehouseAreaId())
                    .eq(StringUtils.isNotBlank(vo.getWarehouseAreaLocationId()), WmsRepertory::getWmsBasicWarehouseAreaLocationId, vo.getWarehouseAreaLocationId())
                    .eq(WmsRepertory::getStatus, "normal"));
        } else {
            list = repertoryService.list(new LambdaQueryWrapper<WmsRepertory>()
                    .eq(WmsRepertory::getWmsBasicWarehouseId, warehouseId)
                    .eq(WmsRepertory::getBatchCode, name)
                    .eq(WmsRepertory::getStatus, "normal"));
            if (CollectionUtils.isEmpty(list)) {
                list = repertoryService.list(new LambdaQueryWrapper<WmsRepertory>()
                        .eq(WmsRepertory::getWmsBasicWarehouseId, warehouseId)
                        .eq(WmsRepertory::getMaterialCode, name)
                        .eq(WmsRepertory::getStatus, "normal"));
            }
        }
        for (WmsRepertory repertory : list) {
            repertory.setRepertoryId(repertory.getId());
            repertory.setId(null);
        }
        return list;
    }

    /**
     * 备料/退料区域库区
     * @param warehouseId
     * @return
     */
    @Override
    public WmsBasicWarehouseArea getMaterialArea(String warehouseId, String id) {
        WmsMoveReceipt move = moveReceiptService.getById(id);
        if (move == null) {
            throw new JeecgBootException("移库单不存在");
        }
        String areaId = "";
        if ("Y103".equals(move.getDocumentType())) {
            areaId = mobileMoveMapper.getInMaterialArea(move.getDeliveryInTaskId(), warehouseId);
        }
        if ("Y106".equals(move.getDocumentType())) {
            areaId = mobileMoveMapper.getOutMaterialArea(move.getDeliveryOutTaskId(), warehouseId);
        }
        WmsBasicWarehouseArea area = warehouseAreaService.getById(areaId);
        return area;
    }
}
